//
//  extensionNavigationButton.swift
//  WeiBo
//
//  Created by Wishing.Star on 2017/1/14.
//  Copyright © 2017年 Wishing.Star. All rights reserved.
//


// 修改Foundation为UIKit
import UIKit
//扩展UIBarButtonItem的方法
extension UIBarButtonItem {
    
    /// 使用便利构造行数进行扩展
    ///
    /// - Parameters:
    ///   - title: string
    ///   - fontSize: 默认16号
    ///   - target: self
    ///   - action: #selector(调用方法)
    ///   - isBack: 判断是否是返回按钮，默认为false，方便只需要在指定的返回按钮上增加相关属性
    convenience init(title: String,fontSize: CGFloat = 16,target: AnyObject?,action: Selector,isBack: Bool = false) {
    //默认导航栏按钮颜色
        let navBtn: UIButton = UIButton.cz_textButton(title, fontSize: fontSize, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)

        navBtn.addTarget(target, action: action, for: .touchUpInside)
        

        //返回按钮需要增加图片
        if isBack {
            let imageName = "navigationbar_back_withtext"
            //设置普通图片
//            navBtn.setImage(UIImage(named: imageName), for: UIControlState(rawValue: 0))
            navBtn.setImage(UIImage(named: imageName), for: UIControlState())
            //设置高亮图片
            navBtn.setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
            
            //出现“<...”，需要重置按钮大小
            navBtn.sizeToFit()
        }
    //将navBtn转换为UIBarButtonItem，并在leftBarButtonItem替换使用
//    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: navBtn)
        //便利构造函数使用self.init()初始化UIBarButtonItem
    self.init(customView: navBtn)
    }
}
